package com.ajoclass.demo.order.service.impl;

import com.ajoclass.demo.order.dao.OrderRepository;
import com.ajoclass.demo.order.entity.Order;
import com.ajoclass.demo.order.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderRepository orderRepository;


    @Override
    public Order findByOrderSn(String orderSn) {
        log.info("find order by orderSn: {}",orderSn);
        return orderRepository.findByOrderSn(orderSn);
    }

    @Override
    public Order save(Order order) {
        return this.orderRepository.save(order);
    }

    @Override
    public Page<Order> findAll(int current, int rowCount) {
        if(current<1){
            return null;
        }
        Pageable pageable =PageRequest.of(--current, rowCount);
        return this.orderRepository.findAll(pageable);
    }

}
